<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns:ui="http://java.sun.com/jsf/facelets"
                template="../../Templates/SubFolderTemplate.xhtml"
                xmlns:p="http://primefaces.prime.com.tr/ui"
                xmlns:h="http://java.sun.com/jsf/html"
                xmlns:f="http://java.sun.com/jsf/core"
                xmlns:c="http://java.sun.com/jsp/jstl/core">

    <ui:define name="head-title">
        <title>Add Delivery Order | Merlion's ERP System</title>
    </ui:define>

    <ui:define name="content">
        <f:view beforePhase="#{addDeliveryOrderManagerBean.initView}">
            <h:form>
                <p:panel id="panel" header="Add New Delivery Order"> 
                    <p:ajaxStatus style="width:16px;height:16px;">   
                        <f:facet name="start">   
                            <p:graphicImage value="http://www.primefaces.org:8080/showcase/design/ajaxloading.gif" /> 
                        </f:facet> 

                        <f:facet name="complete">   
                            <h:outputText value="" />   
                        </f:facet>   
                    </p:ajaxStatus>

                    <p:fieldset legend="Sale Order Detail">

                        <h:panelGrid columns="2" cellpadding="4">

                            <h:outputText value="Sale Order ID:"/>
                            <h:outputText value="#{addDeliveryOrderManagerBean.selectedSaleOrder.id}"/>

                            <h:outputText value="Date:"/>
                            <h:outputText value="#{addDeliveryOrderManagerBean.selectedSaleOrder.saleOrderDate}"/>

                            <h:outputText value="Customer ID:"/>
                            <h:outputText value="#{addDeliveryOrderManagerBean.selectedSaleOrder.customer.registrationNumber}"/>

                            <h:outputText value="Customer Name:"/>
                            <h:outputText value="#{addDeliveryOrderManagerBean.selectedSaleOrder.customer.saleLeadName}"/>
                            
                            <h:outputText value="Customer Discount:"/>
                            <h:outputText value="#{addDeliveryOrderManagerBean.selectedSaleOrder.saleQuotation.customDiscount}"/>

                        </h:panelGrid>

                        <p:dataTable var="lineItem" value="#{addDeliveryOrderManagerBean.selectedSaleOrder.lineItems}"
                                     dynamic="true" emptyMessage="No Product Type" id="display2">
                            <p:column >
                                <f:facet name="header">
                                    <h:outputText value="Product Type"/>
                                </f:facet>
                                <h:outputText value="#{lineItem.productPrice.productType}"/>
                            </p:column>

                            <p:column >
                                <f:facet name="header">
                                    <h:outputText value="Quantity"/>
                                </f:facet>
                                <h:outputText value="#{lineItem.lineItemQuantity}"/>
                            </p:column>

                            <p:column >
                                <f:facet name="header">
                                    <h:outputText value="Price per Unit"/>
                                </f:facet>
                                <h:outputText value="#{lineItem.productPrice.productPrice}"/>
                            </p:column>
                            
                            <p:column >
                                <f:facet name="header">
                                    <h:outputText value="Volume Discount"/>
                                </f:facet>
                                <h:outputText value="#{lineItem.volumeDiscountRate}"/>
                            </p:column>
                            
                            <p:column >
                                <f:facet name="header">
                                    <h:outputText value="Final price"/>
                                </f:facet>
                                <h:outputText value="#{lineItem.finalPrice}"/>
                            </p:column>
                            
                            <f:facet name ="footer">
                                <h:outputText value="Total Price:"/>
                                <h:outputText value ="#{addDeliveryOrderManagerBean.selectedSaleOrder.totalPrice}"/>
                            </f:facet>
                        </p:dataTable>

                    </p:fieldset>

                    <p:spacer height="20"/>
                    
                    <p:messages /> 
                    
                    <p:spacer height="15"/>

                    <p:fieldset legend="Add New Delivery">

                        <h:panelGrid columns="3" cellpadding="2"> 

                            <h:outputLabel for="productType" value="Product Type: " /> 
                            <h:selectOneMenu id="productType" value="#{addDeliveryOrderManagerBean.productType}" >
                                <f:selectItems value="#{addDeliveryOrderManagerBean.displayProductType}"/>
                            </h:selectOneMenu>
                            <p:message for="productType" />

                            <h:outputLabel for="quantity" value="Quantity: " /> 
                            <p:inputText required="true" label="quantity" id="quantity" value="#{addDeliveryOrderManagerBean.quantity}">                         
                            </p:inputText>
                            <p:message for="quantity"/>

                            <p:commandButton id="submit" value="Submit" update="table1" actionListener="#{addDeliveryOrderManagerBean.displayTempList(event)}" ajax="true" />
                            <p:commandButton value="Clear" type="reset"/>

                        </h:panelGrid>

                        <p:spacer height="20"/>

                        <p:dataTable id="table1" var="productLine" value="#{addDeliveryOrderManagerBean.tempProductLines}"
                                     dynamic="true" paginator="true" emptyMessage="No Record Yet!">
                            <p:column sortBy="#{productLine.product.productType}">
                                <f:facet name="header">
                                    <h:outputText value="Product Type"/>
                                </f:facet>
                                <h:outputText value="#{productLine.product.productType}"/>
                            </p:column>

                            <p:column>
                                <f:facet name="header">
                                    <h:outputText value="Quantity"/>
                                </f:facet>
                                <h:outputText value="#{productLine.quantity}"/>
                            </p:column>
                            
                            <p:column>
                                <f:facet name="header">
                                    <h:outputText value="Final price"/>
                                </f:facet>
                                <h:outputText value="#{productLine.finalPrice}"/>
                            </p:column>

                        </p:dataTable>

                        <p:commandButton id="save" value="Create Delivery Order" update="panel" actionListener="#{addDeliveryOrderManagerBean.saveDeliveryOrder(event)}" ajax="true" />

                    </p:fieldset>

                </p:panel> 
            </h:form>
        </f:view>
    </ui:define>

</ui:composition>